package com.seele.tigerwallet.ui.activtiy;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.seele.tigerwallet.R;
import com.seele.tigerwallet.model.WebModel;
import com.seele.tigerwallet.ui.base.TitleBaseActivity;
import com.seele.tigerwallet.ui.base.ToolbarBaseActivity;
import com.seele.tigerwallet.ui.views.Html5Webview;

public class WebViewActivtiy extends ToolbarBaseActivity {


    private Html5Webview mWebView;

    private boolean isDisplay;
    private boolean isHtmlTitle;


    @Override
    public int getContainerViewId() {
        return R.layout.activity_web_view_activtiy;
    }

    @Override
    protected void initView() {
        setTextNormalBar();
        mWebView = $f(R.id.webview);
        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            WebModel webModel = (WebModel) bundle.getSerializable(WebViewActivtiy.class.getSimpleName());
            isDisplay = webModel.isDisplay;
            isHtmlTitle = webModel.isHtmlTitle;
            if (null != webModel) {
                if (!TextUtils.isEmpty(webModel.title)) {
                    setTitle(webModel.title);
                }
                if (!TextUtils.isEmpty(webModel.url)) {
                    mWebView.loadUrl(webModel.url);

                }
                if (isDisplay) {
                    setRightText(R.string.contact_us);
                }
            }
        }


        mWebView.setOnHtml5WebviewListener(new Html5Webview.OnHtml5WebviewListener() {
            @Override
            public void onHtml5Webview(String title) {
                if (isDisplay||isHtmlTitle) {
                    if (!TextUtils.isEmpty(title)) {
                        setTitle(title);
                    }

                }
            }
        });


    }

    @Override
    protected void onBcak() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            finish();
        }
    }

    @Override
    protected void onRightText() {
        startActivity(CustomerActivity.class);

    }




    @Override
    protected void onDestroy() {
        //清空所有Cookie
        CookieSyncManager.createInstance(this);  //Create a singleton CookieSyncManager within a context
        CookieManager cookieManager = CookieManager.getInstance(); // the singleton CookieManager instance
        cookieManager.removeAllCookie();// Removes all cookies.
        CookieSyncManager.getInstance().sync(); // forces sync manager to sync now
        mWebView.setWebChromeClient(null);
        mWebView.setWebViewClient(null);
        mWebView.getSettings().setJavaScriptEnabled(false);
        mWebView.clearCache(true);
        mWebView.clearHistory();
        super.onDestroy();
    }
}

